
package com.netease.xone.fragment;

import media.MediaPlayerWrapper;
import media.MediaPlayerWrapper.MediaListener;
import Utils.StackTraceUtil;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.netease.log.NTLog;
import com.netease.pkgRelated.XoneConsts.MENU_ID;
import com.netease.xone.R;
import com.netease.xone.itemview.RenderAudioPlayView;
import com.netease.xone.widget.appmsg.AppMsg;


public class FragmentAudioBase extends FragmentBase implements MediaListener {
    public static final String TAG = FragmentAudioBase.class.getSimpleName();
    
    private boolean bPlayer = false;
    private AppMsg mAppMsg;

    @Override
    public void onAttach(Activity activity) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        super.onAttach(activity);
        MediaPlayerWrapper.getInstance().registerMediaListener(this);
        if (MediaPlayerWrapper.PlayStatus.PLAYING == MediaPlayerWrapper.getInstance().getPlayStatus()) {
            bPlayer = true;
        }
    }
    
    @Override
    public void onDetach() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        MediaPlayerWrapper.getInstance().removeMediaListener(this);
        super.onDetach();
        mAppMsg = null;
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        if (bPlayer) {
            MenuItem item = menu.add(Menu.NONE, MENU_ID.menu_audio_play, 0, "audio");
            item.setIcon(R.drawable.icon_speaker_1);
            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        }
        super.onCreateOptionsMenu(menu, inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        switch (item.getItemId()) {
            case MENU_ID.menu_audio_play:
                if (null != mAppMsg) {
                    mAppMsg.cancel();
                }
                AppMsg msg = AppMsg.makeText(getActivity(), null, AppMsg.STYLE_INFO);
                View view = View.inflate(getActivity(), R.layout.view_audio_play, null);
                RenderAudioPlayView renderAudioPlayView = new RenderAudioPlayView(view);
                renderAudioPlayView.setOnClickListener(new OnClickListener() {
                    
                    @Override
                    public void onClick(View v) {
                        if (null != mAppMsg) {
                            mAppMsg.cancel();
                        }
                    }
                });
                renderAudioPlayView.renderView(getActivity());
                msg.setView(view);
                msg.setDuration(AppMsg.LENGTH_5S);
                msg.show();
                mAppMsg = msg;
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }
    
    @Override
    public void onMediaPlay() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        if (!bPlayer) {
            bPlayer = true;
            getSherlockActivity().invalidateOptionsMenu();
        }
    }

    @Override
    public void onMediaPause() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
    }
    
    @Override
    public void onMediaRelease() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        bPlayer = false;
        getSherlockActivity().invalidateOptionsMenu();
    }

    @Override
    public void onMediaCompletion() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        MediaPlayerWrapper.getInstance().release();
    }

}
